<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\SpeakingContent;

/**
 * SpeakingContentSearch represents the model behind the search form about `app\models\SpeakingContent`.
 */
class SpeakingContentSearch extends SpeakingContent
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'hear_number', 'question_number', 'class1_id', 'class2_id', 'num', 'type'], 'integer'],
            [['info', 'content', 'diff', 'direction', 'topic_content', 'listening_script', 'listening_content', 'reading_tips', 'reading_content', 'reading_title', 'img_url'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = SpeakingContent::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'hear_number' => $this->hear_number,
            'question_number' => $this->question_number,
            'class1_id' => $this->class1_id,
            'class2_id' => $this->class2_id,
            'num' => $this->num,
            'type' => $this->type,
        ]);

        $query->andFilterWhere(['like', 'info', $this->info])
            ->andFilterWhere(['like', 'content', $this->content])
            ->andFilterWhere(['like', 'diff', $this->diff])
            ->andFilterWhere(['like', 'direction', $this->direction])
            ->andFilterWhere(['like', 'topic_content', $this->topic_content])
            ->andFilterWhere(['like', 'listening_script', $this->listening_script])
            ->andFilterWhere(['like', 'listening_content', $this->listening_content])
            ->andFilterWhere(['like', 'reading_tips', $this->reading_tips])
            ->andFilterWhere(['like', 'reading_content', $this->reading_content])
            ->andFilterWhere(['like', 'reading_title', $this->reading_title])
            ->andFilterWhere(['like', 'img_url', $this->img_url]);

        return $dataProvider;
    }
}
